package se.webbzon.boltzmann.editor.factory.propertyframe;

import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.boltzmann.editor.propertyframe.WorldMovableObjectPropertyFrame;
import se.webbzon.boltzmann.editor.propertyframe.WorldObjectPropertyFrame;
import se.webbzon.boltzmann.object.factory.InstanceFactoryObject;
import se.webbzon.oschi01.worldobject.WorldObject;

public class FactoryObjectPropertyFrameFactory implements PropertyFrameFactory {

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public String getWorldObjectName() {
		return InstanceFactoryObject.worldObjectName;
	}

	@Override public WorldObjectPropertyFrame makePropertyFrame(EditorModel model,
																WorldObject instance) {
		return new PropertyFrameImpl(model,(InstanceFactoryObject) instance);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private static final class PropertyFrameImpl extends WorldMovableObjectPropertyFrame {

		// The input fields for this property frame
		private final JTextField factoryNameInput;
		
		/*============================================================
		Constructors
		============================================================*/
		
		public PropertyFrameImpl(EditorModel model, InstanceFactoryObject instance) {
			super(model, instance);
			
			JPanel factoryPanel = new JPanel();
			factoryPanel.setLayout(new BoxLayout(factoryPanel,BoxLayout.Y_AXIS));
			factoryPanel.setBorder(BorderFactory.createTitledBorder("Factory Data:"));
			factoryPanel.add(new JLabel("Factory Name:"));
			factoryNameInput = new JTextField(instance.getFactoryName());
			factoryPanel.add(factoryNameInput);
			
			addPanel("Factory Settings",factoryPanel);
			initialize();
		}
		
		/*============================================================
		Public Methods
		============================================================*/
		
		@Override public boolean apply(Collection<String> errorMessages) {
			if (super.apply(errorMessages)) try {
				InstanceFactoryObject instance = (InstanceFactoryObject) getInstance();
				instance.setFactoryName(factoryNameInput.getText());
				return true;
			} catch (NumberFormatException e) {
				errorMessages.add(e.getMessage());
				return false;
			} else
				return false;
		}
		
	}

}
